﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.Class;
using Entities;
using BusinessLogicLayer;

namespace QLNhaSach.Modules
{
    public partial class LapBaoCaoCongNo : UserControl
    {
        BindingList<BaoCaoCongNoOBJ> gridDataList = new BindingList<BaoCaoCongNoOBJ>();
        public LapBaoCaoCongNo()
        {
            InitializeComponent();
            DateTime now = DateTime.Now;
            string month = now.Month.ToString();
            string year = now.Year.ToString();
            textEdit1.Text = month;
            textEdit2.Text = year;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            gridDataList.Clear();
            string thang = textEdit1.Text;
            string nam = textEdit2.Text;
            DataTable dt = new DataTable();
            KhachHangBO khbo = new KhachHangBO();
            dt = khbo.layDanhSachKhachHang();
            BaoCaoCongNo bccn = new BaoCaoCongNo();
            bccn.Thang = thang;
            bccn.Nam = nam;
            BaoCaoCongNoBO bccnbo = new BaoCaoCongNoBO();
            int BCCN_MaBaoCaoCongNo = bccnbo.InsertBaoCaoCongNo(bccn);
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                int maKhachHang = int.Parse(row["KH_MAKHACHHANG"].ToString());
                HoaDonBanSachBO hdbo = new HoaDonBanSachBO();
                double tongTienMua = hdbo.GetTongTienCuaKhachHangMuaTrongThang(thang, nam, maKhachHang);
                PhieuThuTienBO pttbo = new PhieuThuTienBO();
                double tongTienTra = pttbo.GetTongTienCuaKhachHangTraTrongThang(thang, nam, maKhachHang);
                double tienNo = double.Parse(row["KH_TIENNO"].ToString());
                gridDataList.Add(new BaoCaoCongNoOBJ(i++.ToString(), row["KH_HOTENKHACHHANG"].ToString(), row["KH_TIENNO"].ToString(), (tongTienMua - tongTienTra).ToString(), (tongTienMua - tongTienTra + tienNo).ToString()));
                ChiTietBaoCaoCongNo ctbccn = new ChiTietBaoCaoCongNo();
                ctbccn.MaBaoCaoCongNo = BCCN_MaBaoCaoCongNo;
                ctbccn.MaKhachHang = maKhachHang;
                ctbccn.NoDau = double.Parse(row["KH_TIENNO"].ToString());
                ctbccn.PhatSinh = tongTienMua - tongTienTra;
                ctbccn.NoCuoi = tongTienMua - tongTienTra + tienNo;
                ChiTietBaoCaoCongNoBO ctbccnbo = new ChiTietBaoCaoCongNoBO();
                ctbccnbo.InsertChiTietBaoCaoCongNo(ctbccn);
            }
            gridControl1.DataSource = gridDataList;
            
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ nhập kí tự số");
                e.Handled = true;
            }
        }

        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ nhập kí tự số");
                e.Handled = true;
            }
        }
    }
}
